from detectEnglish import removeNonLetters,ENGLISH_WORDS
import os

def UpdateDictionary_withString(str_plaintext = ''):
    str_plaintext = str_plaintext.upper()
    str_plaintext = removeNonLetters(str_plaintext)
    possibleWords = str_plaintext.split()
    
    if possibleWords == []:
        return
    
    for word in possibleWords:
        if word not in ENGLISH_WORDS:
            ENGLISH_WORDS[word] = None
            
    dictionaryFile = open('dictionary.txt','w')
    for word in sorted(ENGLISH_WORDS.keys()):
        dictionaryFile.write(word+'\n')
    
    dictionaryFile.close()
    
def UpdateDictionary_withFile(path_sourcesfile):
    if os.path.isfile(path_sourcesfile):
        str_plaintext = open(path_sourcesfile).read()
        UpdateDictionary_withString(str_plaintext)
        
def main():
    UpdateDictionary_withFile('sources/plaintext_changyidian')
    UpdateDictionary_withFile('sources/plaintext_ciphertext')
    UpdateDictionary_withFile('sources/dictionary_lower.txt')
    UpdateDictionary_withFile('sources/textfile.txt')
    
if __name__ == '__main__':
    main()
    